<template>
  <div class="mod-marketing-coupon">
    <div class="search-bar">
      <el-form :inline="true" class="search-form" ref="searchForm" :model="searchForm" size="small" >
        <div class="input-row">
          <el-form-item prop="name" :label="$t('chain.name') + ':'" label-width="80px" class="search-form-item">
            <el-input type="text" v-model="searchForm.name" :placeholder="$t('chain.name')"></el-input>
          </el-form-item>
          <el-form-item prop="code" :label="$t('chain.code') + ':'" label-width="80px" class="search-form-item">
            <el-input type="text" v-model="searchForm.code" :placeholder="$t('chain.code')"></el-input>
          </el-form-item>
          <el-form-item prop="status" :label="$t('chain.status') + ':'" label-width="60px" class="search-form-item">
            <el-select v-model="searchForm.status" :placeholder="$t('chain.status')">
              <el-option :label="$t('chain.enable')" :value="1"></el-option>
              <el-option :label="$t('chain.disable')" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <div class="default-btn primary-btn marginBottom" @click="searchChange(true)">{{$t('product.search')}}</div>
          <div class="default-btn" @click="resetSearchForm('searchForm')">{{$t('product.reset')}}</div>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div
          class="default-btn primary-btn"
          @click.stop="addOrUpdateHandle()">
          {{ $t('sysManagement.add') }}
        </div>
      </div>
      <div class="table-con coupon-table">
        <el-table
          ref="couponListTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%">

          <el-table-column
            fixed
            align="left"
            prop="name"
            :label="$t('chain.name')"
            width="auto">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.name || '-' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            align="left"
            prop="code"
            :label="$t('chain.code')"
            width="auto">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.code || '-' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="icon"
            :label="$t('chain.icon')"
            width="100">
            <template slot-scope="scope">
              <div class="names" style="display: flex;align-items: center">
                <el-image :src="scope.row.icon.indexOf('http')===-1 ? resourcesUrl + scope.row.icon : scope.row.icon" style="width: 50px;height: 50px;margin-right: 15px">
                  <div slot="error" class="image-slot">
                    <img src="@/assets/img/def.png">
                  </div>
                </el-image>
              </div>
            </template>
          </el-table-column>


          <el-table-column
            prop="apiUser"
            :label="$t('chain.apiUser')"
            width="auto"
          >
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.apiUser || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="apiPassword"
            :label="$t('chain.apiPassword')"
            width="auto"
          >
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.apiPassword || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="address"
            :label="$t('chain.address')"
            width="auto">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.address || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="mnemonic"
            :label="$t('chain.mnemonic')"
            width="auto">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.mnemonic || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="middleAddress"
            :label="$t('chain.middleAddress')"
            width="auto">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.middleAddress || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="middleMnemonic"
            :label="$t('chain.middleMnemonic')"
            width="auto">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.middleMnemonic || '-' }}</span>
            </template>
          </el-table-column>

          <el-table-column
            prop="orderNum"
            :label="$t('chain.seq')"
            width="auto">
            <template slot-scope="scope">
              <span class="tag-text">{{scope.row.orderNum}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            :label="$t('chain.status')"
            width="auto">
            <template slot-scope="scope">
              <span v-if="scope.row.status === 1" class="tag-text">{{$t("chain.enable")}}</span>
              <span
                v-else-if="scope.row.status === 0"
                class="tag-text"
              >{{$t("chain.disable")}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            :label="$t('chain.createTime')"
            width="auto">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.createTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="updateTime"
            :label="$t('chain.updateTime')"
            width="auto">
            <template slot-scope="scope">
              <span class="table-cell-text">{{ scope.row.updateTime || '-' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            align="center"
            :label="$t('publics.operating')"
            width="230"
          >
            <template slot-scope="scope">
              <div class="text-btn-con">
                <div
                  class="default-btn text-btn"
                  @click="addOrUpdateHandle(scope.row.id)"
                >{{$t('user.editBtn')}}</div>
                <div
                  class="default-btn text-btn"
                  @click.stop="deleteHandle(scope.row.id)"
                >{{$t('remindPop.delete')}}</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        v-if="dataList.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './add-or-update.vue'
export default {
  data () {
    return {
      // 图片前缀
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      theData: null, // 保存上次点击查询的请求条件
      theParams: null, // 保存上次点击查询的请求条件

      search: {
        slot: ''
      },
      dataForm: {
        name: '',
        code: '',
        status: null
      },
      dateRange: [],
      dataList: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {
        name: '',
        code: '',
        status: ''
      }
    }
  },
  components: {
    AddOrUpdate
  },
  created () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList (page, newData = false) {
      this.dataListLoading = true
      if (this.page) {
        let size = Math.ceil(this.page.total / this.page.pageSize)
        this.page.currentPage = (this.page.currentPage > size ? size : this.page.currentPage) || 1
      }
      if (newData || !this.theData) {
        this.theParams = JSON.parse(JSON.stringify(this.searchForm))
        this.theData = {
          current: page == null ? this.page.currentPage : page.currentPage,
          size: page == null ? this.page.pageSize : page.pageSize,
          'name': this.dataForm.name,
          'code': this.dataForm.code,
          'status': this.dataForm.status
        }
      } else {
        this.theData.current = page == null ? this.page.currentPage : page.currentPage
        this.theData.size = page == null ? this.page.pageSize : page.pageSize
      }

      this.$http({
        url: this.$http.adornUrl('/platform/blockchain/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            this.theData,
            this.theParams
          ), false
        )
      }).then(({ data }) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },

    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('remindPop.makeSure') + ' ' + `[${id ? this.$i18n.t('remindPop.delete') : this.$i18n.t('remindPop.batchDeletion')}]` + ' ' + this.$i18n.t('remindPop.operation') + '?', this.$i18n.t('remindPop.remind'), {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/platform/blockchain/' + id),
            method: 'delete',
            data: this.$http.adornData()
          }).then(({ data }) => {
            this.page.total = this.page.total - 1
            this.$message({
              message: this.$i18n.t('remindPop.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.refreshChange()
              }
            })
          })
        })
        .catch(() => { })
    },
    // 条件查询
    searchChange (newData = false) {
      this.getDataList(this.page, newData)
    },
    // 刷新回调用
    refreshChange () {
      this.getDataList(this.page)
    },
    // 多选变化
    selectionChange (val) {
      this.dataListSelections = val
    },
    /**
     * 重置表单
     * @param {String} formName 表单名称
     */
    resetSearchForm (formName) {
      this.$refs[formName].resetFields()

      this.getDataList(this.page,this.theParams) //重置表单加载初始列表
    },

    handleSizeChange (val) {
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.getDataList()
    }
  }
}
</script>
<style lang="scss" scoped>
.marginBottom{margin-bottom:20px}
</style>
